package cn.wolfcode.wolf2w.business.listen;

import cn.wolfcode.wolf2w.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class TravelListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private RedisService redisService;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        //根据需求查询近期数据
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {
            String key = RedisKeys.STRATEGY_STATUS_HASH.join(strategy.getId().toString());
            if (!redisService.hasKey(key)){
                Map<String, Object> map = new HashMap<>();
                map.put("viewnum",strategy.getViewnum());
                map.put("replynum",strategy.getReplynum());
                map.put("favornum",strategy.getFavornum());
                map.put("sharenum",strategy.getSharenum());
                map.put("thumbsupnum",strategy.getThumbsupnum());
                map.put("id",strategy.getId());
                redisService.setCacheMap(key,map);
            }
        }
        log.info("攻略模块数据初始化成功");
    }
}
